import { defineStore } from 'pinia'
import { reqCategory1, reqCategory2, reqCategory3 } from '@/api/product/attr'
import type { categoryResponseData } from '@/api/product/attr/types'
import type { categoryState } from './types/type'

let useCategoryStore = defineStore('category', {
  state: (): categoryState => {
    return {
      c1Arr: [],
      c2Arr: [],
      c3Arr: [],
      categoryIdObj: {
        category1Id: '',
        category2Id: '',
        category3Id: '',
      },
    }
  },
  actions: {
    async getC1() {
      let res: categoryResponseData = await reqCategory1()
      if (res.code === 200) {
        this.c1Arr = res.data
      }
    },
    async getC2() {
      let res: categoryResponseData = await reqCategory2(
        this.categoryIdObj.category1Id,
      )
      if (res.code === 200) {
        this.c2Arr = res.data
      }
    },
    async getC3() {
      let res: categoryResponseData = await reqCategory3(
        this.categoryIdObj.category2Id,
      )
      if (res.code === 200) {
        this.c3Arr = res.data
      }
    },
  },
})

export default useCategoryStore
